<?php
use module\home\model\User;
use module\home\model\Share;
/*
 * data helper class for displaying onto the views
 * 
 * various data related common used functions
 * static class, should not be created.  All functions are available as static
 *
 * since most of the retrive functions are put here, this class needs to be heavily optimized
 * currently there is no optimization done
 *
 * @package yasns
 * @subpackage library
 */
class DataHelper
{
	private static 
		$_loginUserFriends = array();
	
	/**
	 * get the recent user list based limited by count
	 * 
	 * if count is not stated, it will generate all
	 * 
	 * @param int $count
	 * @return array 
	 */
	public static function getRecentUserList($count = null)
	{
		$userList = User::findByRecent($count);
		
		if (!$userList) { return null; }
		
		return $userList;
	}

	/** 
	 * retrieve the count for friends who added user
	 * 
	 * @return int 
	 */
	public static function getFriendAddedUserCount()
	{
		if (!Auth::hasIdentity()) { return null; }
		
		$user = Auth::getIdentity();
		$account_id = $user->account_id;
		$friends = User::findFriendsAddedUser($account_id);
		
		if (!$friends) { $count = 0; }
		
		$count = count($friends);
		
		return $count;
	}	
	
	/**
	 * retrieve a list of friends who added user 
	 * 
	 * if user is logged in, it will locate user's account id
	 * and find people who added user
	 * 
	 * @param int $count
	 * @return array
	 */
	public static function getFriendsAddedUserList($count = null)
	{
		if (!Auth::hasIdentity()) { return null; }
		
		$user = Auth::getIdentity();
		$account_id = $user->account_id;
		$friendList = User::findFriendsAddedUser($account_id);
		
		if (!$friendList) { return null; }
		
		return $friendList;
	}
	
	/**
	 * get the user's friend list (friends where users added as friend)
	 * 
	 * if user is logged in, it will locate user's account id
	 * and find user's friends
	 * 
	 * @param int $count
	 * @return array 
	 */
	public static function getFriendList($count = null)
	{
		if(self::$_loginUserFriends === null) {
			// first time we will friend list
		
		}
		if (!Auth::hasIdentity()) { return array(); }
		
		$user = Auth::getIdentity();
		$account_id = $user->account_id;
		$friendList = User::findFriends($account_id, $count);

		if (!$friendList) { return null; }
		
		return $friendList;
	}
	
	/**
	 * retrieve the count for user's friends
	 * @return int 
	 */
	public static function getFriendsCount()
	{
		if (!Auth::hasIdentity()) { return null; }
		
		$user = Auth::getIdentity();
		$account_id = $user->account_id;
		$friendList = User::findFriends($account_id);
		
		if (!$friendList) { $count = 0; }
		
		$count = count($friendList);
		
		return $count;
	}
	
	/**
	 * retrieve all recent activities / shares 
	 * 
	 * @param int $count
	 * @return array
	 */
	public static function getAllRecentActivities($count = null, $type = null)
	{
		if($type) {
			$activities = Share::findByType($type, $count);
		} else {
			$activities = Share::findAll($count);
		}
		
		if (!$activities) { return array(); }
		
		return $activities;
	}

	/**
	 * retrieve user's last status message
	 * 
	 * @param string $share_type
	 * @param int $account_id
	 * @param int $count
	 * @return array
	 */
	public static function getShareMessage($share_type, $account_id, $count = null)
	{
		$conn = DataModel::getConnection();
		$table = "share";
		$fieldShareType = "share_type";
		$fieldAccountId = "account_id";
		$sortby = "share_datetime DESC";

		// check if count is empty
		$limit = (isset($count)) ? "limit {$count}" : '';
		
		$stmt = $conn->prepare(	"select * from {$table} " . 
										"where {$fieldAccountId} = ? ".
										"AND {$fieldShareType} = ? ".
										"ORDER BY {$sortby} {$limit}");
										
		$stmt->execute( array($account_id, $share_type) );
		$row = $stmt->fetch();
		
		if (!$row) { return null; }

		return $row;
	}
	
	
	/**
	 * format date
	 * 
	 * @param date $date
	 * @return string 
	 */
	public static function setDateFormat($date)
	{
		if ($date) {
			$date = date("M dS, Y g:ia", strtotime($date));
		}
		return $date;
	}
	
	public static function getRelationshipWithLoggedInUser($account_id)
	{
		if(!Auth::hasIdentity()) return 0; // user is not logged in
		
		$loginUser = Auth::getIdentity();
		if($loginUser->account_id == $account_id) return 9; // same user
		
		$friends = DataHelper::getFriendList();
		$friendList = array();
		// put all friends in a list for checking
		if (!empty($friends)) {
			foreach ($friends as $friend) {
				$friendList[] = $friend['account_friend_id'];
			}

		}
		
		if(in_array($account_id, $friendList)) {
			return 1; // friend!
		}
		
		return -1; // not friend
	}
	
}
?>